package com.yyk.yiliao.ui.find.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.ui.gongyong.activity.WebViewActivity;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.bean.HomeArticlelist;
import com.yyk.yiliao.util.rx.RxUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 搜索医院医生
 */
public class FindSearch_Activity extends BaseActivity {


    @BindView(R.id.nHome_location)
    LinearLayout nHomeLocation;
    @BindView(R.id.mSearch_int)
    EditText mSearchInt;
    @BindView(R.id.mSearch_clean)
    ImageView mSearchClean;
    @BindView(R.id.mSearch_sousuo)
    TextView mSearchSousuo;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.mSearch_rv)
    RecyclerView mSearchRv;
    @BindView(R.id.mSearch_no)
    TextView mSearchNo;
    @BindView(R.id.ll_center_bg)
    LinearLayout llCenterBg;
    @BindView(R.id.mSearch)
    LinearLayout mSearch;
    @BindView(R.id.mSearchFl)
    FrameLayout mSearchFl;
    private BaseRecyclerAdapter<HomeArticlelist.DataBean> adapter;
    private String request;
    private List<HomeArticlelist.DataBean> data = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.activity_find_search;
    }

    @Override
    public void initView() {
        initEditText();
        initToolbar();
    }
    private void initToolbar() {
        hidetoolBar();
    }
    @Override
    public void initData() {
        initAdapter();
    }

    private void initAdapter() {
        adapter = new BaseRecyclerAdapter<HomeArticlelist.DataBean>(FindSearch_Activity.this, data, R.layout.adapter_item_hospitaldoctor) {
            @Override
            public void convert(BaseRecyclerHolder holder, HomeArticlelist.DataBean item, int position, boolean isScrolling) {
                holder.setText(R.id.b_hospital_name, item.getTitle());
            }
        };
//            mSearchRv.addItemDecoration(new DividerItemDecoration(FindSearch_Activity.this, DividerItemDecoration.VERTICAL));
        mSearchRv.setLayoutManager(new LinearLayoutManager(FindSearch_Activity.this, LinearLayoutManager.VERTICAL, false));
        mSearchRv.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                Intent intent = new Intent(FindSearch_Activity.this, WebViewActivity.class);
                intent.putExtra("toolbar_tv", "发现详情");
                intent.putExtra("url", ApiService.H5_URL + "views/newDiscover/DiscoverInfo.html?tid=" + data.get(position).getId() + "&Sources=3?from=app");
                startActivity(intent);
            }
        });
    }

    /**
     * 文本框搜索医院或者医生
     */
    private void setmSearch(Map<String, String> map, final boolean flag) {
        RxUtils.getInstance().retrofit.create(ApiService.class).postHomeArticlelist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeArticlelist>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();

                    }

                    @Override
                    public void onNext(HomeArticlelist info) {
                        Logger.e("HomeArticlelist" + info.toString());
                        if (info.getCode() == 1) {
                            //                            mSearchRv.setVisibility(View.VISIBLE);
                            //                            llCenterBg.setVisibility(View.GONE);

                            if (flag) {
                                data.clear();
                            }

                            if (null != info.getData() && info.getData().size() > 0) {
                                data.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();

                        } else {
                            //                            mSearchRv.setVisibility(View.GONE);
                            //                            llCenterBg.setVisibility(View.VISIBLE);
                            ToastUtil.showShort(FindSearch_Activity.this, "抱歉，没有找到相关的信息");
                        }
                    }


                });

    }

    private void initEditText() {

        mSearchInt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (!TextUtils.isEmpty(s.toString())) {
                    mSearchClean.setVisibility(View.VISIBLE);
                } else {
                    mSearchClean.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s.toString())) {
                    mSearchClean.setVisibility(View.GONE);
                } else {
                    mSearchClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(mSearchInt.getText().toString())) {
                    mSearchClean.setVisibility(View.GONE);
                } else {
                    mSearchClean.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @OnClick({R.id.mSearch_int, R.id.mSearch_clean, R.id.mSearch_sousuo, R.id.iv_back})
    public void onViewClicked(View view) {
        request = mSearchInt.getText().toString().trim();

        switch (view.getId()) {
            case R.id.mSearch_int:

                break;
            case R.id.mSearch_clean:
                mSearchInt.setText("");
                break;
            case R.id.mSearch_sousuo:
                Map<String, String> map2 = new TreeMap<>();
                map2.put("name", request);
                map2.put("sign", Md5Util2.createSign(map2));
                setmSearch(map2, true);
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }


}
